//
//  CommentViewController.m
//  BuffetNews
//
//  Created by 李荃荃 on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CommentViewController.h"
//#import <Comment/Comment.h>
#import "WebCell.h"
#import "DetialViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "NightModel.h"
#import "SwitchButtonCell.h"
#import "SliderButtonCell.h"
#import "SegmentCell.h"
#import "WebColor.h"
#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

@interface CommentViewController ()<UIScrollViewDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>
@property (nonatomic, retain)UIWebView *web;
@property (nonatomic, retain)UIView *modeView;
@property (nonatomic, retain)UIView *hiddeView;
//@property (nonatomic, assign)NSInteger open;
@property (nonatomic, retain)NightModel *night;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation CommentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _night = [NightModel shareInstance];
    }
    return self;
}
- (void)dealloc
{
    [_web release];
    [_modeView release];
    [_hiddeView release];
    [_night release];
    [_tableView release];
    [_webString release];
    [_pkString release];
    [_tittle release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"******   %d", [NightModel shareInstance].open);
    
    BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
    
    if (!reachable) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(5, self.view.frame.size.height / 13 * 12, self.view.frame.size.width / 7, self.view.frame.size.height / 14 );
        [button setBackgroundImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(block:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"该功能需要连接网络才能使用，请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
        
        
        
    } else {
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height + 4)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webString]];
    [_web loadRequest:request];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.web.frame.size.width, 20)];

    imageView.image = [UIImage imageNamed:@"白条.png"];
    _web.scalesPageToFit = YES;
    self.navigationController.navigationBarHidden =YES;
    self.web.scrollView.bounces = NO;

//    [NSURL URLWithString:self.webString];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(30, self.view.frame.size.height - 32, 32, 32);
    [button setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tagerButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_web];
    self.web.scrollView.delegate = self;
    [self.view addSubview: imageView];
    [self.view addSubview:button];
    // 按钮
    
    UIButton *buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonShare.frame = CGRectMake(100, self.view.frame.size.height - 32, 32, 32);
    [buttonShare setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
    [buttonShare addTarget:self action:@selector(tagerButton1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonShare];
    
    
    
    UIButton *buttonSet = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonSet.frame = CGRectMake(170, self.view.frame.size.height - 32, 32, 32);
    [buttonSet setImage:[UIImage imageNamed:@"c.png"] forState:UIControlStateNormal];
    [buttonSet addTarget:self action:@selector(tagerButton2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonSet];
    
    
    UIButton *buttonTitile = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonTitile.frame = CGRectMake(250, self.view.frame.size.height - 32, 32, 32);
    [buttonTitile setImage:[UIImage imageNamed:@"d.png"] forState:UIControlStateNormal];
    [buttonTitile addTarget:self action:@selector(tagerButton3:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonTitile];
    

    if ([NightModel shareInstance].open == 1) {
        self.view .backgroundColor = [UIColor blackColor];
    }else{
        self.view.backgroundColor = [UIColor whiteColor];
    }
    _web.delegate = self;
    
    [_web release];
    
    [imageView release];
    }
}
- (void)block:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(BOOL)isReachableViaWWAN
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

- (void)tagerButton3:(UIButton *)button
{
    DetialViewController *detialView = [[DetialViewController alloc]init];
    detialView.pkString = self.pkString;
    detialView.tittle = self.tittle;
    [detialView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:detialView animated:YES completion:^{
        
    }];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"我看看%d",_night.open);
        if (_night.open == 1) {
            [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.img.backgroundColor= 'black'"];
            [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
            [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= 'black'"];
        }else{
            [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= 'white'"];
        }

}
- (void)tagerButton2:(UIButton *)button
{
    static int count = 1;
    if (count == 1) {
        _modeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 32)];
        _modeView.backgroundColor = [UIColor blackColor];
        _modeView.alpha = 0.5;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCelnce:)];
        [_modeView addGestureRecognizer:tap];
        [self.view addSubview:_modeView];
        [tap release];
        
        _hiddeView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 52, self.view.frame.size.width, 120 + 20)];
        _hiddeView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_hiddeView];
        [UIView animateWithDuration:0.3 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            _hiddeView.frame = CGRectMake(0, self.view.frame.size.height - 120 - 52, self.view.frame.size.width, 120 + 20);
        }];
        [UIView animateWithDuration:0.6 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            _modeView.backgroundColor = [UIColor blackColor];
            _modeView.alpha = 0.5;
        }];
        _tableView = [[UITableView alloc]initWithFrame:_hiddeView.bounds];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.bounces = NO;
        [_hiddeView addSubview:_tableView];
        [_tableView release];
        count = 2;
    }else if (count == 2){
        [_hiddeView removeFromSuperview];
        [_modeView removeFromSuperview];
        count = 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 47;
}
- (void)tapCelnce:(UITapGestureRecognizer *)tap
{
    [_hiddeView removeFromSuperview];
    [_modeView removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        SwitchButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
        if (cell == nil) {
            cell = [[[SwitchButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"a"]autorelease];
        }

        cell.Mylabel.text = @"夜间模式";
        
        cell.Mylabel.font = [UIFont boldSystemFontOfSize:12];
        
        [cell.switchView addTarget:self action:@selector(switchTap:) forControlEvents:UIControlEventValueChanged];
        
        if (_night.open == 1) {
            cell.switchView.on = YES;
        }else{
            cell.switchView.on = NO;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([NightModel shareInstance].open == 1) {
            cell.backgroundColor = [WebColor darkSlateGray];
            cell.Mylabel.textColor = [UIColor grayColor];
        }else{
            cell.backgroundColor = [UIColor whiteColor];
            cell.Mylabel.textColor = [UIColor blackColor];
        }
        return cell;

    }else if(indexPath.row == 1){
        SliderButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"b"];
        if (cell == nil) {
            cell = [[[SliderButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"b"]autorelease];
        }
        cell.Mylabel.text = @"屏幕亮度";
        cell.Mylabel.font = [UIFont boldSystemFontOfSize:12];
        [cell.slider addTarget:self action:@selector(sliderTap:) forControlEvents:UIControlEventValueChanged];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([NightModel shareInstance].open == 1) {
            cell.backgroundColor = [WebColor darkSlateGray];
            cell.Mylabel.textColor = [UIColor grayColor];
        }else{
            cell.backgroundColor = [UIColor whiteColor];
            cell.Mylabel.textColor = [UIColor blackColor];
            
        }
        return cell;

    }else{
        SegmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
        if (cell == nil) {
            cell = [[[SegmentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"c"]autorelease];
        }

        cell.Mylabel.text = @"字体大小";
        cell.Mylabel.font = [UIFont boldSystemFontOfSize:12];
        [cell.segement addTarget:self action:@selector(sementTap:) forControlEvents:UIControlEventValueChanged];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


        if ([NightModel shareInstance].open == 1) {
            cell.backgroundColor = [WebColor darkSlateGray];
            cell.Mylabel.textColor = [UIColor grayColor];
        }else{
            cell.backgroundColor = [UIColor whiteColor];
            cell.Mylabel.textColor = [UIColor blackColor];

        }
        return cell;
    }

}
- (void)sliderTap:(UISlider *)slider
{
    [[UIScreen mainScreen] setBrightness: slider.value];
}
- (void)switchTap:(UISwitch *)switchView
{
    if (switchView.on == YES) {
        [NightModel shareInstance].open = 1;

        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"Night" object:@"1"];
        
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.img.backgroundColor= 'black'"];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= 'black'"];
        _night.open = 1;
        self.view.backgroundColor = [UIColor blackColor];
        
            }else{
                [NightModel shareInstance].open = 0;
                NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
                [center postNotificationName:@"Night" object:@"1"];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.img.backgroundColor= 'white'"];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        [_web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.backgroundColor= 'white'"];
        _night.open = 0;
        self.view.backgroundColor = [UIColor whiteColor];

    }
    [_tableView reloadData];
}
- (void)sementTap:(UISegmentedControl *)sement
{
    NSInteger Index = sement.selectedSegmentIndex;
    switch (Index) {

            case 0:
        {
            NSString *str1 =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",80.0];
            [_web stringByEvaluatingJavaScriptFromString:str1];
        }
            break;
            
        case 1:
        {
            NSString *str2 =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",110.0];
            [_web stringByEvaluatingJavaScriptFromString:str2];
        }
            break;
            
        case 2:
        {
            NSString *str2 =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",120.0];
            [_web stringByEvaluatingJavaScriptFromString:str2];
        }

            break;
            
        case 3:
        {
            NSString *str2 =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",150.0];
            [_web stringByEvaluatingJavaScriptFromString:str2];
        }

            break;
            
        default:
            
            break;
            
    }
    

}
- (void)tagerButton1:(UIButton *)button
{
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK"  ofType:@"jpg"];
    
    //构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                       defaultContent:@"默认分享内容，没内容时显示"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"ShareSDK"
                                                  url:@"http://www.sharesdk.cn"
                                          description:@"这是一条测试信息"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    [ShareSDK showShareActionSheet:nil
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions: nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(@"分享成功");
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                                }
                            }];
}
- (void)tagerButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y == 0) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
